package com.javatest.springboot2basictest.bean;

import java.util.Objects;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.LifecycleProcessor;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2020-04-14 下午12:32
 */
@Slf4j
@Data
public class JuiceMaker implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean, LifecycleProcessor {
    private String beverageShop = null;
    private volatile boolean isRunning;

    @Autowired
    private Source source;

    public JuiceMaker() {
        log.warn("[" + this.getClass().getSimpleName() + "]执行构造器方法");
    }

    private void init() {
        log.warn("[" + this.getClass().getSimpleName() + "]执行自定义初始化方法init");
    }

    private void myDestory() {
        log.warn("[" + this.getClass().getSimpleName() + "]执行自定义销毁方法");
    }

    public String makeJuice() {
        String juice = "这是一杯由" + beverageShop + "饮品店提供的"
                + source.getSize()
                + source.getSuger()
                + source.getFruit();
        return juice;
    }

    @PostConstruct
    public void testPostConstruct() {
        log.warn("[" + this.getClass().getSimpleName() + "]调用@PostConstruct注解的方法, 此时source是否为空？" + Objects.isNull(source));
    }

    @PreDestroy
    public void testPreDestroy() {
        log.warn("[" + this.getClass().getSimpleName() + "]调用@PreDestroy注解的方法");
    }

    @Override
    public void setBeanName(String s) {
        log.warn("[" + this.getClass().getSimpleName() + "]调用BeanNameAware的setBeanName方法");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.warn("[" + this.getClass().getSimpleName() + "]调用BeanFactoryAware的setBeanFactory方法");
    }

    @Override
    public void destroy() throws Exception {
        log.warn("[" + this.getClass().getSimpleName() + "]调用DisposableBean的destroy方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.warn("[" + this.getClass().getSimpleName() + "]调用InitializingBean的afterPropertiesSet方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.warn("[" + this.getClass().getSimpleName() + "]调用ApplicationContextAware的setApplicationContext方法");
    }

    @Override
    public void onRefresh() {
        log.warn("[" + this.getClass().getSimpleName() + "]调用LifecycleProcessor的onRefresh方法");
        this.isRunning = true;
    }

    @Override
    public void onClose() {
        log.warn("[" + this.getClass().getSimpleName() + "]调用LifecycleProcessor的onClose方法");
        this.isRunning = false;
    }

    @Override
    public void start() {
        log.warn("[" + this.getClass().getSimpleName() + "]调用Lifecycle的start方法");
        this.isRunning = true;
    }

    @Override
    public void stop() {
        log.warn("[" + this.getClass().getSimpleName() + "]调用Lifecycle的stop方法");
        this.isRunning = false;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    /*@Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        log.warn("[" + this.getClass().getSimpleName() + "]调用BeanFactoryPostProcessor的postProcessBeanFactory方法");
    }*/
}
